#!/system/bin/sh
#!/usr/bin/env bash
# 用户级保活
# APK名称
ApkName=cloud-phone.apk
# APK包名
PackageName=com.cloud.phone
# Service
ServiceName=com.cloud.phone/.service.CloudService

InstallService() {
        count=`pm list packages | grep $PackageName |wc -l`
        if [ $count -le 0 ]; then
                echo "$ApkName is not installed, now install."
                ret=`pm install $ApkName`
                Succ="Success"
                if [ "$ret" == "$Succ" ]; then
                        echo "install succeed."
                else
                        echo "install failed."
                fi
        else
                echo "$ApkName is already installed."
                echo "Service start by boot_complete broadcast."
        fi
        return 0
}

StartService() {
    ret=`am startservice $ServiceName`
    Err="Error: Not found; no service started."
        contain=$(echo $ret | grep "${Err}")
        if [[ "$contain" != "" ]]; then
                echo "Service start failed."
                #exit 1
        else
                echo "Service start succeed."
        fi
        return 0
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone

# 定义查询应用是否运行的命令
AM_PID_CMD="ps -ef|grep $APP_PACKAGE|grep -v grep"

# 定义拉起应用的命令
LAUNCH_APP_CMD="am start $APP_PACKAGE/.MainActivity"

# 间隔时间（秒）
SLEEP_TIME=10

StartActivity(){
   ${LAUNCH_APP_CMD}
   echo "应用正在运行..."
}

# 设置后台进程名称
SERVICE_NAME=com.cloud.phone/.service.CloudService

# 定义查询后台进程是否运行的命令
AM_SERVICE_CMD="ps -ef|grep $SERVICE_NAME|grep -v grep"

# 定义拉起后台进程的命令
LAUNCH_SERVICE_CMD="am start-foreground-service $SERVICE_NAME"

# 间隔时间（秒）
SLEEP_TIME=30

CheckForegroundService(){
    while true; do
      sleep ${SLEEP_TIME}
       if [ -z "$(${AM_SERVICE_CMD})" ]; then
        echo "后台服务没有运行，尝试拉起..."
         ${LAUNCH_SERVICE_CMD}
       else
        echo "后台服务正在运行..."
        #若后台服务正在运行exit 1 退出 continue继续.
        #exit 0
      fi
    done
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone

# 定义app后台被杀死白名单
ADD_BG_KILLER_WHITE_APP="dumpsys cph_performance addBgKillerWhiteApp $APP_PACKAGE"

# 定义app自动拉起白名单
ADD_AUTO_RUN_WHITE_APP="dumpsys cph_performance addAutoRunWhiteApp $APP_PACKAGE"

AddWhiteKillApp(){
  ${ADD_BG_KILLER_WHITE_APP}
  ${ADD_AUTO_RUN_WHITE_APP}
}

# 定义应用被杀死广播的参数
KILL_ACTION="android.intent.action.APP_KILLED"
# 使用adb发送广播
LAUNCH_KILL_APP_BROADCAST="am broadcast -a $KILL_ACTION"

StartKillAppBroadcast(){
     ${LAUNCH_KILL_APP_BROADCAST}
     # 检查是否成功发送
     if [ $? -eq 0 ]; then
        echo "开机广播成功发送"
    else
        echo "开机广播发送失败"
    fi
}

# vpn服务名称
VPN_SERVICE_NAME=com.cloud.phone/.service.CloudVpnService

# 定义查询后台vpn服务进程是否运行的命令
AM_VPN_SERVICE_CMD="ps -ef|grep $VPN_SERVICE_NAME|grep -v grep"

# 定义拉起后台vpn服务进程的命令
LAUNCH_VPN_SERVICE_CMD="am start-foreground-service $VPN_SERVICE_NAME"

# 间隔时间（秒）
SLEEP_TIME=10

StartCloudVpnSerVice(){
    while true; do
      sleep ${SLEEP_TIME}
       if [ -z "$(${AM_VPN_SERVICE_CMD})" ]; then
        echo "后台vpn服务没有开启，尝试开启..."
         ${LAUNCH_VPN_SERVICE_CMD}
       else
        echo "后台vpn服务正在运行..."
      fi
    done
}

# 设置应用包名
APP_PACKAGE=com.cloud.phone
# 默认开启应用root权限
OPEN_APP_ROOT="cmd activity add-root-permission ${APP_PACKAGE}"

AutoAppRoot(){
  ${OPEN_APP_ROOT}
}

# 定义开启无障碍的服务
AUTO_ACCESSIBILITY_SERVICE_NAME=com.cloud.phone/.service.AccessibilityAutoMainService
#设置无障碍服务是否开启
ENABLED_ACCESSIBILITY_SERVICES="settings put secure enabled_accessibility_services $AUTO_ACCESSIBILITY_SERVICE_NAME"
#设置自动开启无障碍服务
OPEN_ACCESSIBILITY_ENABLED="settings put secure accessibility_enabled 1"

AutoAccessibilityService(){
 ${ENABLED_ACCESSIBILITY_SERVICES}
 ${OPEN_ACCESSIBILITY_ENABLED}
}

# 设置目标文件
file="extend_custom.sh"
# 设置目标权限
permissions="750"

chmodFilePermission(){
  # 检查文件是否存在
  if [ -f "$file" ]; then
      # 修改文件权限
      chmod "$permissions" "$file"
      echo "已修改权限：$file"
  else
      echo "文件不存在：$file"
  fi
}

main() {
        # 安装应用
        InstallService
        echo "To start when first installed."

        #将应用加入白名单
        AddWhiteKillApp

        # 启动服务
        StartService
        echo "To start service for cloudService."

        # 拉起应用
        StartActivity
        echo "start activity for cloudPhone."

        # 检查后台进程是否存在
        CheckForegroundService
        echo "check service is running."

        # 开启应用被杀死广播
        StartKillAppBroadcast
        echo "Kill app Broadcast send "

        # 开启vpn服务
        #StartCloudVpnSerVice
        echo "start vpn service."

        # 默认开启root权限
        AutoAppRoot
        echo "cloud phone open root"

        # 开启无障碍服务权限
        AutoAccessibilityService
        echo "auto open accessibility service"

        # 修改脚本文件权限
        chmodFilePermission
        echo "chmod file permission"
}

main

